<!-- 裁剪月产量 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <!-- <h1 class="f16">节假日定义</h1> -->
        <Table
          :outTableHeight="10"
          :summaryData="summaryData"
          summary
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
      </el-card>
    </div>
    <el-drawer v-model="drawer" :direction="'ttb'" size="small" :with-header="false">
      <span @click="drawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <div class="flex AIC" style="flex-direction: column">
        <div class="flex mt10 JCC" style="width: 100%; background-color: white">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
            <div class="flex">
              <h1 class="f16"></h1>
              <el-button type="primary" size="default" @click="exportExcel">导出</el-button>
            </div>
            <Table :summaryData="summaryData9" summary :tableHeader="tableHeader9" :tableData="tableData9" class="mt10" />
          </el-card>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { onMounted, ref } from 'vue'
import { watch } from 'vue'
import { debounce } from 'lodash'
import service from '@/utils/request'
import fileRequest from '@/utils/fileRequest'
export default {
  name: 'CutMonthlyOutput'
}
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const tableHeader = ref([
  {
    width: '55px',
    change: true,
    label: '姓名',
    prop: 'workerName'
  },
  {
    width: '55px',
    label: '组别',
    prop: 'line'
  },
  {
    width: '45.5px',
    label: '大货',
    prop: 'dhQty'
  },
  {
    width: '45.5px',
    label: '试版',
    prop: 'sbQty'
  },
  {
    blue: true,
    width: '45.5px',
    label: '样品',
    prop: 'ypQty'
  },
  {
    blue: true,
    width: '45.5px',
    label: '合计',
    prop: 'total'
  },
  {
    blue: true,
    width: '45.5px',
    label: '1',
    prop: 'day1'
  },
  {
    blue: true,
    width: '45.5px',
    label: '2',
    prop: 'day2'
  },
  {
    blue: true,
    width: '45.5px',
    label: '3',
    prop: 'day3'
  },
  {
    blue: true,
    width: '45.5px',
    label: '4',
    prop: 'day4'
  },
  {
    blue: true,
    width: '45.5px',
    label: '5',
    prop: 'day5'
  },
  {
    blue: true,
    width: '45.5px',
    label: '6',
    prop: 'day6'
  },
  {
    blue: true,
    width: '45.5px',
    label: '7',
    prop: 'day7'
  },
  {
    blue: true,
    width: '45.5px',
    label: '8',
    prop: 'day8'
  },
  {
    blue: true,
    width: '45.5px',
    label: '9',
    prop: 'day9'
  },
  {
    blue: true,
    width: '45.5px',
    label: '10',
    prop: 'day10'
  },
  {
    blue: true,
    width: '45.5px',
    label: '11',
    prop: 'day11'
  },
  {
    blue: true,
    width: '45.5px',
    label: '12',
    prop: 'day12'
  },
  {
    blue: true,
    width: '45.5px',
    label: '13',
    prop: 'day13'
  },
  {
    blue: true,
    width: '45.5px',
    label: '14',
    prop: 'day14'
  },
  {
    blue: true,
    width: '45.5px',
    label: '15',
    prop: 'day15'
  },
  {
    blue: true,
    width: '45.5px',
    label: '16',
    prop: 'day16'
  },
  {
    blue: true,
    width: '45.5px',
    label: '17',
    prop: 'day17'
  },
  {
    blue: true,
    width: '45.5px',
    label: '18',
    prop: 'day18'
  },
  {
    blue: true,
    width: '45.5px',
    label: '19',
    prop: 'day19'
  },
  {
    blue: true,
    width: '45.5px',
    label: '20',
    prop: 'day20'
  },
  {
    blue: true,
    width: '45.5px',
    label: '21',
    prop: 'day21'
  },
  {
    blue: true,
    width: '45.5px',
    label: '22',
    prop: 'day22'
  },
  {
    blue: true,
    width: '45.5px',
    label: '23',
    prop: 'day23'
  },
  {
    blue: true,
    width: '45.5px',
    label: '24',
    prop: 'day24'
  },
  {
    blue: true,
    width: '45.5px',
    label: '25',
    prop: 'day25'
  },
  {
    blue: true,
    width: '45.5px',
    label: '26',
    prop: 'day26'
  },
  {
    blue: true,
    width: '45.5px',
    label: '27',
    prop: 'day27'
  },
  {
    blue: true,
    width: '45.5px',
    label: '28',
    prop: 'day28'
  },
  {
    blue: true,
    width: '45.5px',
    label: '29',
    prop: 'day29'
  },
  {
    blue: true,
    width: '45.5px',
    label: '30',
    prop: 'day30'
  },
  {
    blue: true,
    width: '45.5px',
    label: '31',
    prop: 'day31'
  }
])
const getCurrentYearMonth = () => {
  const date = new Date()
  const year = date.getFullYear().toString() // 获取年份的后两位
  const month = (date.getMonth() + 1).toString().padStart(2, '0') // 获取月份并补零
  return `${year}${month}`
}
const formList = ref([
  {
    dateType: 'month',
    type: 'dateMonth',
    dateLabel: '月份',
    dateValue: getCurrentYearMonth()
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/statements/downloadStitchMonthProd?month=${formList.value[0].dateValue}&prodSeq=${3}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 2,
    num: 0
  },
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  }
])
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  },
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 10,
    num: 0
  },
  {
    index: 11,
    num: 0
  },
  {
    index: 12,
    num: 0
  },
  {
    index: 13,
    num: 0
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 15,
    num: 0
  },
  {
    index: 16,
    num: 0
  },
  {
    index: 17,
    num: 0
  },
  {
    index: 18,
    num: 0
  },
  {
    index: 19,
    num: 0
  },
  {
    index: 20,
    num: 0
  },
  {
    index: 21,
    num: 0
  },
  {
    index: 22,
    num: 0
  },
  {
    index: 23,
    num: 0
  },
  {
    index: 24,
    num: 0
  },
  {
    index: 25,
    num: 0
  },
  {
    index: 26,
    num: 0
  },
  {
    index: 27,
    num: 0
  },
  {
    index: 28,
    num: 0
  },
  {
    index: 29,
    num: 0
  },
  {
    index: 30,
    num: 0
  },
  {
    index: 31,
    num: 0
  },
  {
    index: 32,
    num: 0
  },
  {
    index: 33,
    num: 0
  },
  {
    index: 34,
    num: 0
  },
  {
    index: 35,
    num: 0
  },
  {
    index: 36,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.get<any, any>(
    `/statements/getStitchMonthProd?month=${
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '月份')?.dateValue
    }&prodSeq=${3}`
  )
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    for (const key in item) {
      if (item[key] === 0) {
        item[key] = ''
      }
    }
    item.blue == true
    return item
  })
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.stitch.dQty
  summaryData.value[1].num = tableDataList.stitch.sQty
  summaryData.value[2].num = tableDataList.stitch.zQty
  summaryData.value[3].num = tableDataList.stitch.total

  summaryData2.value.forEach((item: any, index: number) => {
    item.num = tableDataList.stitch[`day${index + 1}`]
  })
  summaryData.value = summaryData.value.concat(summaryData2.value)
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const choose = ref(0)
const currentProp = ref('')
const currentRow = async (row: any = tableData.value[0], scop: any = {}, e: any = {}, prop: string = '') => {
  currentProp.value = prop
  if (prop.includes('day')) {
    choose.value = 2
  }
  if (prop.includes('total')) {
    choose.value = 1
  }
  if (choose.value !== 0 && e.target.innerText && e.target.innerText !== '0') {
    drawer.value = true
    getTableList2(row)
  }
  currentTableRow.value = row
}
const drawer = ref(false)
const imgList = ref([])
const tableHeader9 = ref([
  {
    width: 120,
    label: '生产日期',
    prop: 'prodTime'
  },
  {
    width: 180,
    label: '指令号',
    prop: 'lotNo'
  },
  {
    width: 180,
    type: 'img',
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    srcList: imgList
  },
  {
    width: 120,
    label: '品牌',
    prop: 'brand'
  },
  {
    width: 120,
    label: '样品类型',
    prop: 'orderType'
  },
  {
    width: 160,
    label: '工厂货号',
    prop: 'customStyle'
  },
  {
    width: 120,
    label: '颜色',
    prop: 'styleColor'
  },
  {
    width: 120,
    label: '号码',
    prop: 'sizeNo'
  },
  {
    width: 120,
    label: '生产双数',
    prop: 'prodQty'
  }
])
const tableData9 = ref([])
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange2 = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData9 = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  }
])
const getTableList2 = debounce(async (row: any) => {
  let month = formList.value[0].dateValue
  if (choose.value === 2) {
    const day2 = currentProp.value.split('day')[1]
    month = month + day2.padStart(2, '0')
  }
  isLoading.value = true
  let tableDataList = await service.get<any, any>(
    `/statements/getCurrentMonth?choose=${choose.value}&month=${month}&prodSeq=${3}&workerId=${row.workerId}`
  )
  imgList.value = tableDataList.map((item: any) => {
    return item.photoPath
  })
  isLoading.value = false
  tableData9.value = tableDataList
}, 500)
const exportExcel = async () => {
  let month = formList.value[0].dateValue
  if (choose.value === 2) {
    const day2 = currentProp.value.split('day')[1]
    month = month + day2.padStart(2, '0')
  }

  const res = (await fileRequest.post(
    `/statements/downloadStitchMonthProdTotal?choose=${choose.value}&month=${month}&prodSeq=${3}&workerId=${
      currentTableRow.value.workerId
    }&workerName=${currentTableRow.value.workerName}`
  )) as any
  const link = document.createElement('a') // 利用a标签自定义下载文件名
  let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
  link.style.display = 'none'
  //设置连接
  link.href = window.URL.createObjectURL(blob)
  const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
  link.download = FileName //自定义文件名字
  document.body.appendChild(link)
  //模拟点击事件
  link.click()
  document.body.removeChild(link)
}
</script>
